#include "QQVLCApi.h"

QQVLCApi::QQVLCApi(QObject* parent)
	:QObject(parent)
{
	m_audio_loop = 0;
	m_volume = 10;

	m_instance = libvlc_new(0, nullptr);
	m_player = nullptr;
	m_media = nullptr;
	if (!m_instance)
	{
		qWarning() << "Failed to create libvlc instance.";
		return;
	}
	m_player = libvlc_media_player_new(m_instance);
	if (!m_player)
	{
		qWarning() << "Failed to create media player.";
		libvlc_release(m_instance);
		m_instance = nullptr;
		return;
	}
	m_media = nullptr;
	libvlc_audio_set_volume(m_player, 100);
}

QQVLCApi::~QQVLCApi()
{
	if (m_media)
		libvlc_media_release(m_media);
	if (m_player)
		libvlc_media_player_release(m_player);
	if (m_instance)
		libvlc_release(m_instance);
}

void QQVLCApi::set_audio_loop(int loop)
{
	m_audio_loop = loop;
}

int QQVLCApi::get_audio_loop() const
{
	return m_audio_loop;
}

void QQVLCApi::set_volume(int volume)
{
	if (volume < 0 || volume > 100)
	{
		qWarning() << "Volume value" << volume << "is out of range. Valid range is 0 to 100.";
		return;
	}

	if (m_player)
	{
		int result = libvlc_audio_set_volume(m_player, m_volume);
		if (result == -1)
		{
			qWarning() << "Failed to set volume to" << m_volume;
		}
		else
		{
			m_volume = volume;
			qInfo() << "Volume successfully set to" << m_volume;
		}
	}
	else
	{
		qWarning() << "Attempted to set volume but media player is not initialized.";
	}
}

int QQVLCApi::get_volume() const
{
	return m_volume;
}

void QQVLCApi::play_audio_by_file(const QString& filename)
{
	if (m_media)
	{
		libvlc_media_release(m_media);
		m_media = nullptr;
	}

	QString fileurl = "file:///" + filename;
	m_media = libvlc_media_new_location(m_instance, fileurl.toStdString().c_str());
	if (!m_media)
	{
		qWarning() << "Failed to create media for URL:" << fileurl;
		return;
	}
	qDebug() << "Media created for URL:" << fileurl;

	if (libvlc_media_player_is_playing(m_player))
	{
		libvlc_media_player_stop(m_player);
		qDebug() << "Playback stopped due to new media being loaded.";
	}

	libvlc_media_add_option(m_media, QString("input-repeat=%1").arg(m_audio_loop).toStdString().c_str());
	qDebug() << "Option 'input-repeat' set to" << m_audio_loop;
	libvlc_media_player_set_media(m_player, m_media);
	if (libvlc_media_player_play(m_player) != 0)
	{
		qWarning() << "Failed to start playback.";
	}
	else
	{
		qDebug() << "Playback started successfully.";
	}
}

void QQVLCApi::pause_audio()
{
	if (m_player && libvlc_media_player_is_playing(m_player))
	{
		libvlc_media_player_set_pause(m_player, true);
		qDebug() << "Audio paused.";
	}
	else
	{
		qWarning() << "Attempted to pause audio but it is not playing or media player is not initialized.";
	}
}

void QQVLCApi::resume_audio()
{
	if (m_player && !libvlc_media_player_is_playing(m_player))
	{
		libvlc_media_player_set_pause(m_player, false);
		qDebug() << "Audio resumed.";
	}
	else
	{
		qWarning() << "Attempted to resume audio but it is already playing or media player is not initialized.";
	}
}

void QQVLCApi::stop_audio()
{
	if (m_player)
	{
		if (libvlc_media_player_is_playing(m_player))
		{
			libvlc_media_player_stop(m_player);
			qDebug() << "Audio stopped.";
		}
		else
		{
			qDebug() << "No audio is currently playing.";
		}

		if (m_media)
		{
			libvlc_media_release(m_media);
			m_media = nullptr;
			qDebug() << "Released associated media.";
		}
	}
	else
	{
		qWarning() << "Attempted to stop audio but media player is not initialized.";
	}
}

bool QQVLCApi::is_playing() const
{
	if (m_player)
	{
		return libvlc_media_player_is_playing(m_player);
	}
	else
	{
		qWarning() << "Attempted to check if audio is playing but media player is not initialized.";
	}
}

void QQVLCApi::stop_audio_event(const libvlc_event_t* event, void* data)
{
	libvlc_media_player_t* player = static_cast<libvlc_media_player_t*>(data);
	if (!player)
	{
		qWarning() << "Invalid player pointer in stop_audio_event.";
		return;
	}

	if (libvlc_media_player_is_playing(player))
	{
		qDebug() << "Stopping playback due to event.";
		libvlc_media_player_stop(player);
	}
	else
	{
		qDebug() << "No audio is currently playing.";
	}
	libvlc_media_t* media = libvlc_media_player_get_media(player);
	if (media)
	{
		qDebug() << "Releasing associated media.";
		libvlc_media_release(media);
	}
	else
	{
		qDebug() << "No associated media found.";
	}
}
